<?php
namespace Admin\Controller;
use Think\Controller;
class BaseController extends Controller {
   function _initialize(){
        if(session('admin')===null){
           $this->redirect('Reg/login');
       }else{
            if(session('type')!=1){
                $this->rootCheck();
            }
        }
   }
   protected function rootCheck(){
       $department_id=session('admin_info.department_id');
       $m = M('department');
	   $r = M('root');
	   
       $root_group_id=$m->where(array('id'=>$department_id))->getField('root_group_id');
       $a_id=$r->where(array('controller'=>CONTROLLER_NAME))->getField('action,id',true);
       $group_id=explode(',',$root_group_id);
       $no_arr=array(	//额外的都具备的权限
           'Index'	=>	array('index','welcome'),
           'Role'	=>	array('getDepartment','getRole','getEditRole'),
       );
       foreach($a_id as $k=>$v){
           $key=explode(',',$k);
		   foreach($key as $ks=>$vs){
			   if($vs==ACTION_NAME){
				   $root_id=$v;
				   $is_check=$r->where(array('id'=>$v))->getField('is_check');
				   if($is_check){//1：验证权 0：不验证权限（默认）
					    if(!in_array($root_id,$group_id)){
						   if(!in_array(ACTION_NAME,$no_arr[CONTROLLER_NAME])){
							   alert(0,'没有权限');
						   }
						}
				   }
			   }
		   }
       }
   }
}